{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfcff211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "● 句柄.ipynb - tools - Visual Studio Code\n",
      "句柄-找点.txt - 记事本\n",
      "Anaconda PowerShell Prompt\n",
      "第10周.xlsx - Excel\n",
      "媒体播放器\n",
      "媒体播放器\n",
      "\n",
      "设置\n",
      "设置\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "Microsoft Text Input Application\n",
      "Anaconda PowerShell Prompt\n",
      "按键精灵9.61（52pojie）\n",
      "Anaconda PowerShell Prompt\n",
      "桌面\n",
      "Crack\n",
      "Crack\n",
      "OriginPro 9.1 64-bit - \\曲线平均实例 * - /Folder1/ - [Book1]\n",
      "AutoDL创建虚拟环境，并在Jupyter notebook中使用_autodl 虚拟环境-CSDN博客 和另外 2 个页面 - 个人 - Microsoft​ Edge\n",
      "tools: 小工具 和另外 1 个页面 - 个人 - Microsoft​ Edge\n",
      "● test.ipynb - pyautogui - Visual Studio Code\n",
      "OriginPro 9.1 64-bit - \\UNTITLED * - /Folder1/\n",
      "lab - JupyterLab - 个人 - Microsoft​ Edge\n",
      "豆包 - 字节跳动旗下 AI 智能助手 - 豆包\n",
      "\n",
      "\n",
      "Program Manager\n"
     ]
    }
   ],
   "source": [
    "import pygetwindow as gw\n",
    "# 获取所有窗口\n",
    "all_windows = gw.getAllWindows()\n",
    "# 遍历所有窗口并打印标题\n",
    "for window in all_windows:\n",
    "    print(window.title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "90beb351",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "窗口句柄: 66954\n",
      "窗口位置: (-42, 261)\n",
      "窗口大小: 2197x1417\n",
      "程序开始运行，将在5秒内检测鼠标位置。\n",
      "鼠标在窗口内的相对坐标: (2127, 383)\n",
      "鼠标在窗口内的相对坐标: (2123, 385)\n",
      "鼠标在窗口内的相对坐标: (1850, 476)\n",
      "鼠标在窗口内的相对坐标: (1616, 546)\n",
      "鼠标在窗口内的相对坐标: (1254, 556)\n",
      "鼠标在窗口内的相对坐标: (1030, 545)\n",
      "鼠标在窗口内的相对坐标: (918, 532)\n",
      "鼠标在窗口内的相对坐标: (844, 514)\n",
      "鼠标在窗口内的相对坐标: (794, 501)\n",
      "鼠标在窗口内的相对坐标: (794, 501)\n",
      "鼠标在窗口内的相对坐标: (794, 501)\n",
      "鼠标在窗口内的相对坐标: (794, 501)\n",
      "鼠标在窗口内的相对坐标: (794, 501)\n",
      "鼠标在窗口内的相对坐标: (790, 485)\n",
      "鼠标在窗口内的相对坐标: (784, 460)\n",
      "鼠标在窗口内的相对坐标: (776, 452)\n",
      "鼠标在窗口内的相对坐标: (753, 441)\n",
      "鼠标在窗口内的相对坐标: (687, 546)\n",
      "鼠标在窗口内的相对坐标: (681, 584)\n",
      "鼠标在窗口内的相对坐标: (680, 611)\n",
      "鼠标在窗口内的相对坐标: (680, 624)\n",
      "鼠标在窗口内的相对坐标: (676, 644)\n",
      "鼠标在窗口内的相对坐标: (675, 648)\n",
      "鼠标在窗口内的相对坐标: (670, 645)\n",
      "鼠标在窗口内的相对坐标: (660, 640)\n",
      "鼠标在窗口内的相对坐标: (656, 637)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (651, 635)\n",
      "鼠标在窗口内的相对坐标: (678, 635)\n",
      "鼠标在窗口内的相对坐标: (752, 637)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (762, 638)\n",
      "鼠标在窗口内的相对坐标: (646, 626)\n",
      "鼠标在窗口内的相对坐标: (635, 625)\n",
      "5秒检测结束。\n"
     ]
    }
   ],
   "source": [
    "import pyautogui\n",
    "import win32gui\n",
    "import time\n",
    "\n",
    "# 根据窗口标题查找目标窗口\n",
    "target_title = \"豆包 - 字节跳动旗下 AI 智能助手 - 豆包\"  # 替换为目标窗口的实际标题\n",
    "hwnd = win32gui.FindWindow(None, target_title)\n",
    "\n",
    "if hwnd:\n",
    "    print(f\"窗口句柄: {hwnd}\")\n",
    "\n",
    "    # 获取窗口的位置和大小\n",
    "    rect = win32gui.GetWindowRect(hwnd)\n",
    "    window_x, window_y, window_x2, window_y2 = rect\n",
    "    window_width = window_x2 - window_x\n",
    "    window_height = window_y2 - window_y\n",
    "\n",
    "    print(f\"窗口位置: ({window_x}, {window_y})\")\n",
    "    print(f\"窗口大小: {window_width}x{window_height}\")\n",
    "\n",
    "    print(\"程序开始运行，将在5秒内检测鼠标位置。\")\n",
    "\n",
    "    # 设置计时器\n",
    "    start_time = time.time()\n",
    "    duration = 5  # 持续时间5秒\n",
    "\n",
    "    while time.time() - start_time < duration:\n",
    "        # 获取当前鼠标位置\n",
    "        screen_x, screen_y = pyautogui.position()\n",
    "\n",
    "        # 检查鼠标是否在窗口内\n",
    "        if window_x <= screen_x <= window_x2 and window_y <= screen_y <= window_y2:\n",
    "            # 计算相对于窗口左上角的坐标\n",
    "            relative_x = screen_x - window_x\n",
    "            relative_y = screen_y - window_y\n",
    "            print(f\"鼠标在窗口内的相对坐标: ({relative_x}, {relative_y})\")\n",
    "        else:\n",
    "            print(\"鼠标不在目标窗口内\")\n",
    "\n",
    "        # 稍作延时，避免输出过快\n",
    "        time.sleep(0.1)\n",
    "\n",
    "    print(\"5秒检测结束。\")\n",
    "else:\n",
    "    print(\"未找到目标窗口\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a193ba2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "jq",
   "language": "python",
   "name": "jq"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
